﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AddressBook
{
    public partial class FormEdit : Form
    {
        private readonly AddressBookServices services = new AddressBookServices();

        /// <summary>
        /// 当前记录的id
        /// </summary>
        private Guid id;

        public FormEdit()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 从上级窗体导入id
        /// </summary>
        /// <param name="id"></param>
        public FormEdit(Guid id)
        {
            InitializeComponent();
            var item = services.GetById(id);
            if (item != null)
            {
                txtName.Text = item.Name;
                txtPhoneNo.Text = item.PhoneNo;
                txtEmail.Text = item.Email;
                txtMemo.Text = item.Memo;
                this.id = id;
            }
        }

        private void FormEdit_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 取消操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            T001AddressBookEditModel model = new T001AddressBookEditModel()
            {
                Id = this.id,
                Name = txtName.Text,
                PhoneNo = txtPhoneNo.Text,
                Email = txtEmail.Text,
                Memo = txtMemo.Text,
            };
            services.Edit(model);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
